<template>
    <div class="row product-list">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <ol class="breadcrumb">
                        <li><a>基本设置</a></li>
                        <li><a>微信设置</a></li>
                    </ol>
                </div>
                <div class="content-wrapper">
                    <el-form :inline="true" :model="formData" class="demo-form-inline" size="medium" :rules="rules"
                             ref="ruleForm">
                        <el-row>
                            <el-col :span="8" :offset="4">
                                <el-form-item label="微信appid" :label-width="formLabelWidth" prop="appid">
                                    <el-input v-model="formData.appid" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="8" :offset="4">
                                <el-form-item label="微信secret" :label-width="formLabelWidth" prop="secret">
                                    <el-input v-model="formData.secret" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="8" :offset="4">
                                <el-form-item label="商户号" :label-width="formLabelWidth" prop="roleName">
                                    <el-input v-model="formData.mchid" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="8" :offset="4">
                                <el-form-item label="商户密钥" :label-width="formLabelWidth" prop="roleName">
                                    <el-input v-model="formData.mchkey" auto-complete="off"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="6" :offset="4">
                                <div class="" style="float: right;margin-right: 20px">
                                    <a class="revision-btn" href="javaScript:void(0)" @click="onSubmitForm">保存</a>
                                </div>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

    export default {
        name: 'Wechatconfig',
        components: {},
        data() {
            return {
                formLabelWidth: '120px',
                formData: {
                    id: 0,
                    appid: '',
                    secret: '',
                    mchid: '',
                    mchkey: '',
                },
                rules: {
                    appid: [
                        {required: true, message: '请输入微信appid', trigger: 'blur'}
                    ],
                    secret: [
                        {required: true, message: '请输入微信secret', trigger: 'blur'}
                    ]
                },
                data: {},
                id: ''
            }
        },
        mounted: function () {
            this.getWechatCfg();
        },
        methods: {
            async getWechatCfg() {
                let res = await this.$api.get('/wechatsettings/info/1', {});
                if (res.code === 0) {
                    this.formData = res.wechatSettings;
                } else {
                    this.$message.error(res.msg);
                }
            },
            async updateWechatCfg() {
                let res = await this.$api.post('/wechatsettings/update', this.formData);
                if (res.code === 0) {
                    this.getWechatCfg();
                    this.$message.success('保存成功');
                } else {
                    this.$message.error(res.msg);
                }
            },
            onSubmitForm() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        this.updateWechatCfg();
                    } else {
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .product-list .content-wrapper /deep/ .el-form-item__label {
        color: #666;
        font-size: 12px;
    }
</style>
